﻿using System;
using System.Collections.Generic;
using System.Linq;
using JDPresentShow.Model;
using System.Data;
using System.Text;
using System.Reflection;

namespace JDPresentShow.Pages
{
    public partial class GiftShow : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            };
        }

        private void BindData()
        {
            DateTime dtstart = DateTime.MinValue;
            DateTime dtend = DateTime.MinValue;
      
            if (tbStart.Text.Trim() != string.Empty)
                dtstart = DateTime.Parse(tbStart.Text.Trim());
            if (tbEnd.Text.Trim() != string.Empty)
                dtend = DateTime.Parse(tbEnd.Text.Trim()).AddDays(1);
          
            GIFTDCDataContext giftdc = new GIFTDCDataContext();
            List<ViewGiftApply> list = (from u in giftdc.ViewGiftApply
                                        where (tbkeshi.Text.Trim() == string.Empty ||
                                            (tbkeshi.Text.Trim() != string.Empty && u.ApplierDepartment.Contains(tbkeshi.Text.Trim())))
                                             &&u.Status == "已领取" && u.DealTime !=null &&
                                            (tbStart.Text.Trim() == string.Empty ||
                                            (tbStart.Text.Trim() != string.Empty && u.DealTime >= dtstart))
                                            && (tbEnd.Text.Trim() == string.Empty ||
                                            (tbEnd.Text.Trim() != string.Empty && u.DealTime <= dtend))
                                        orderby u.DealTime descending
                                        select u).ToList();
            rptInfo.DataSource = list;
            rptInfo.DataBind();


        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindData();
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<table border='1'>");
            Guid dept = Guid.Empty;

            
             
            Dictionary<string, string> dic = new Dictionary<string, string>();

            dic.Add("applierdepartment", "申请科室");
            dic.Add("applytime", "申请时间");
            dic.Add("applierdsname", "申 请 人");
            dic.Add("applytitle", "申请标题");
            dic.Add("giftname", "礼品名称");
            dic.Add("giftprice", "礼品价格");
            dic.Add("giftcount", "礼品数量");
            dic.Add("gifttotal", "礼品总价");
            dic.Add("remark", "备注");
            dic.Add("remark2", "备注2");
            DateTime dtstart = DateTime.MinValue;
            DateTime dtend = DateTime.MinValue;
            if (tbStart.Text.Trim() != string.Empty)
                dtstart = DateTime.Parse(tbStart.Text.Trim());
            if (tbEnd.Text.Trim() != string.Empty)
                dtend = DateTime.Parse(tbEnd.Text.Trim()).AddDays(1);
            string a = "";

            if (tbkeshi.Text.Trim() != string.Empty)
            {
                a = tbkeshi.Text.Trim();
            }
            GIFTDCDataContext giftdc = new GIFTDCDataContext();
            
            List<ViewGiftApply> list = (from u in giftdc.ViewGiftApply
                                        where (tbkeshi.Text.Trim() == string.Empty ||
                                            (tbkeshi.Text.Trim() != string.Empty && u.ApplierDepartment.Contains(a)))
                                             && u.Status == "已领取" && u.DealTime != null &&
                                            (tbStart.Text.Trim() == string.Empty ||
                                            (tbStart.Text.Trim() != string.Empty && u.DealTime >= dtstart))
                                            && (tbEnd.Text.Trim() == string.Empty ||
                                            (tbEnd.Text.Trim() != string.Empty && u.DealTime <= dtend))
                                        orderby u.DealTime descending
                                        select u).ToList();

            PropertyInfo[] pis = typeof(ViewGiftApply).GetProperties();
            sb.Append("<tr>");
            foreach (KeyValuePair<string, string> pair in dic)
            {


                foreach (PropertyInfo p in pis)
                {
                    if (p.Name.ToLower() == pair.Key.ToLower())
                    {

                        sb.AppendFormat("<td>{0}</td>", dic[p.Name.ToLower()]);
                        break;
                    }
                }
            }
            sb.Append("</tr>");


            foreach (ViewGiftApply item in list)
            {
                sb.Append("<tr>");
                foreach (KeyValuePair<string, string> pair in dic)
                {
                    foreach (PropertyInfo p in pis)
                    {
                        foreach (KeyValuePair<string, string> pair2 in dic)
                        {
                            if (p.Name.ToLower() == pair.Key.ToLower() && pair2.Key.ToLower() == pair.Key.ToLower())
                            {
                                sb.AppendFormat("<td>{0}</td>", p.GetValue(item, null));
                                break;
                            }
                        }

                    }
                }
                sb.Append("</tr>");
            }
            sb.Append("</table>");
            DateTime dtnow = DateTime.Now;
            string strfile = dtnow.ToShortDateString() + " " + dtnow.ToLongTimeString() + dtnow.Minute.ToString();

            Response.AppendHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode("公务接待信息导出" + strfile + ".xls"));
            Response.Charset = "UTF8";

            Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
            Response.ContentType = "application/ms-excel";
            byte[] bys = System.Text.Encoding.UTF8.GetBytes(sb.ToString());
            byte[] results = new byte[bys.Length + 3];
            results[0] = (byte)0xEF;
            results[1] = (byte)0xBB;
            results[2] = (byte)0xBF;
            Array.Copy(bys, 0, results, 3, bys.Length);
            Response.BinaryWrite(results);
            Response.End();
        }
    }
}
